package cn.tedu.user.management.service.impl;

import cn.tedu.rtms.commons.ex.ServiceException;
import cn.tedu.rtms.commons.web.ServiceCode;
import cn.tedu.user.management.dao.persist.repository.IRoleRepository;
import cn.tedu.user.management.dao.persist.repository.IUserRepository;
import cn.tedu.user.management.dao.persist.repository.IUserRoleRepository;
import cn.tedu.user.management.pojo.entity.UserRole;
import cn.tedu.user.management.pojo.param.UserRoleUpdateParam;
import cn.tedu.user.management.pojo.vo.RoleListItemVO;
import cn.tedu.user.management.service.IRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    IRoleRepository roleRepository;
    @Autowired
    IUserRoleRepository userRoleRepository;
    @Autowired
    private IUserRepository userRepository;

    @Override
    public List<RoleListItemVO> roleList() {
        log.debug("开始处理【查询角色名称列表】的业务");
        return roleRepository.roleList();
    }

    @Override
    public void updateRoleById(Long id, UserRoleUpdateParam userRoleUpdateParam, Long currentId) {
        log.debug("开始处理【修改角色】的业务，ID：{}，新数据：{}, 当前账号id:{}", id, userRoleUpdateParam, currentId);

        //判断当前账号权值与目标对象权值
        Integer weight = userRepository.getWeightById(id);
        Integer currentWeight = userRepository.getWeightById(currentId);
        if (currentWeight == null) {
            String message = "操作失败，您的权限不足！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_FORBIDDEN, message);
        }
        if (weight == null || currentWeight > weight) {
            updateRoleById(id,userRoleUpdateParam);
            return;
        }
        String message = "操作失败，您的权限不足！";
        log.warn(message);
        throw new ServiceException(ServiceCode.ERROR_FORBIDDEN, message);
    }

    private void updateRoleById(Long id, UserRoleUpdateParam userRoleUpdateParam) {
        log.debug("开始处理【修改角色】的业务，ID：{}，新数据：{}", id, userRoleUpdateParam);

        //判断数据是否存在
        int count = roleRepository.countByUserId(id);
        if(count!=1) {
            String message = "查询员工角色失败，员工数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        //获取role_id
        Long newRoleId = userRoleUpdateParam.getRoleId()[0];
        //查看原有role_id
        Long roleId = userRoleRepository.getRoleIdByUserId(id);
        //必要的判断
        if(roleId==null || newRoleId==null) {
            String message = "修改错误，数据为空！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        if(roleId.equals(newRoleId)) {
            String message = "数据未改变，未进行修改！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        //新建对象
        UserRole userRole = new UserRole();
        userRole.setUserId(id);
        userRole.setRoleId(newRoleId);
        //执行修改
        int rows = userRoleRepository.update(userRole);
        if(rows!=1){
            String message = "修改失败！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }

    }

}
